var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
//获取应用实例
var app = getApp();
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
// pages/fenxiao/wdDetail.js
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    app, app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    fxStaticResPath: cf.config.staticResPath + "/mobile/image/fx/",
    wdRecord: [],
    isLoading: false,
    isBottom: false,
    modalSta: [true, true],
    headPic: "",//头像
    enableWithdrawMoney: 0,//可提取
    nickName: "",//昵称
    snickName: "",//昵称
    alipayAccount: "",
    bankAccount: "",
    bankName: "",
    isBind: false,
    page: 1,
    total: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.loadData(false);
    wx.hideShareMenu();
    let that = this;
    that.setData({
      headPic: wx.getStorageSync('employee').headPic,
      snickName: wx.getStorageSync('employee').nickName,
    })
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      that.getPromoterAccount();
      that.loadData(false, that.data.page);
    });

  },
  loadData: function (more, page) {
    wx.showLoading({
      title: "加载中",
    });
    let list = this.data.wdRecord;
    let cxt = this;
    cxt.setData({
      isLoading: true
    });
    if (cxt.data.total == list.length) {
      cxt.setData({
        isBottom: true
      });
      cxt.setData({
        isLoading: false
      });
      wx.hideLoading();
      return;
    }

    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/salesperformance/queryWithdrawRecords",
      data: {
        cusmallToken: cusmallToken,
        start: (page - 1) * 10,
        limit: 10
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        let data = res.data;
        if (data && 0 == data.ret) {
          if (more) {

            let records = data.model.records;
            for (let i = 0; i < records.length; i++) {
              records[i].applyTime = util.formatTime(new Date(records[i].applyTime));
              if (-1 == records[i].state) {
                records[i].stateTxt = "打款失败";
                records[i].staDesc = "请您绑定微信实名认证";
              } else if (0 == records[i].state) {
                records[i].stateTxt = "待商家审批";
                records[i].staDesc = "我们会尽快处理哟";
              } else if (1 == records[i].state) {
                records[i].stateTxt = "打款中";
                records[i].staDesc = "-";
              } else if (2 == records[i].state) {
                if (1 == records[i].operateType) {
                  records[i].stateTxt = "微信打款";
                } else if (2 == records[i].operateType) {
                  records[i].stateTxt = "支付宝打款";
                } else if (3 == records[i].operateType) {
                  records[i].stateTxt = "银行卡打款";
                }
                records[i].staDesc = records[i].transactionId;
              } else if (3 == records[i].state) {
                records[i].stateTxt = "申请被驳回";
                records[i].staDesc = records[i].remark;
              }
            }
            list = list.concat(records);
            cxt.setData({
              wdRecord: list
            });
          } else {
            cxt.setData({
              isBottom: false
            });
            let records = data.model.records;
            for (let i = 0; i < records.length; i++) {
              records[i].applyTime = util.formatTime(new Date(records[i].applyTime));

              if (-1 == records[i].state) {
                records[i].stateTxt = "打款失败";
                records[i].staDesc = "请您绑定微信实名认证";
              } else if (0 == records[i].state) {
                records[i].stateTxt = "待商家审批";
                records[i].staDesc = "我们会尽快处理哟";
              } else if (1 == records[i].state) {
                records[i].stateTxt = "打款中";
                records[i].staDesc = "-";
              } else if (2 == records[i].state) {
                if (1 == records[i].operateType) {
                  records[i].stateTxt = "微信打款";
                } else if (2 == records[i].operateType) {
                  records[i].stateTxt = "支付宝打款";
                } else if (3 == records[i].operateType) {
                  records[i].stateTxt = "银行卡打款";
                }

                records[i].staDesc = records[i].transactionId;
              } else if (3 == records[i].state) {
                records[i].stateTxt = "申请被驳回";
                records[i].staDesc = records[i].remark;
              }
            }
            cxt.setData({
              wdRecord: records
            });
            cxt.setData({
              total: data.model.total
            });
            if (data.model.total == records.length) {
              cxt.setData({
                isBottom: true
              });

              return;
            }
          }
        }
      },
      fail: function () {
      },
      complete: function () {
        cxt.setData({
          isLoading: false
        });
        wx.hideLoading();
      }
    });


  },
  handleMClose: function () {
    let modalSta = this.data.modalSta;
    for (let i in modalSta) {
      modalSta[i] = true;
    }
    this.setData({
      modalSta: modalSta
    });
  },
  handleMShow: function (e) {

    let idx = e.currentTarget.dataset.modal;
    if (1 == idx && !this.data.isBind) {
      idx = 0;
    }
    let modalSta = this.data.modalSta;
    for (let i in modalSta) {
      if (i == idx) {
        modalSta[i] = false;
      } else {
        modalSta[i] = true;
      }
    }
    this.setData({
      modalSta: modalSta
    });
  },
  bindPay: function (e) {
    let that = this;
    let name = e.detail.value.name;
    let phone = e.detail.value.phone;
    let zhifubao = e.detail.value.zhifubao;
    let bank = e.detail.value.bank;
    let bankCard = e.detail.value.bankCard;
    if (!name) {
      wx.showModal({
        title: "提示",
        content: "请填写名字",
        showCancel: false
      });
      return;
    }
    if (!phone) {
      wx.showModal({
        title: "提示",
        content: "请填联系电话",
        showCancel: false
      });
      return;
    }
    if (!zhifubao) {
      wx.showModal({
        title: "提示",
        content: "请填写支付宝帐号",
        showCancel: false
      });
      return;
    }
    if (!bank) {
      wx.showModal({
        title: "提示",
        content: "请填写银行名称",
        showCancel: false
      });
      return;
    }
    if (!bankCard) {
      wx.showModal({
        title: "提示",
        content: "请填写银行卡号",
        showCancel: false
      });
      return;
    }

    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/salesperformance/saveConfig",
      data: {
        cusmallToken: cusmallToken,
        alipay: zhifubao,
        bankName: bank,
        bankAccount: bankCard,
        name: name,
        tel: phone
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        let data = res.data;
        if (data && 0 == data.ret) {
          wx.showModal({
            title: "提示",
            content: "保存成功",
            showCancel: false,
            success: function (res) {
              that.setData({
                modalSta: [true, true]
              });
              that.getPromoterAccount();
            }
          });
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    });

  },
  wdMoney: function (e) {
    let that = this;
    let inputMoney = e.detail.value.inputMoney;
    console.log(inputMoney)
    if (!inputMoney) {
      wx.showModal({
        title: "提示",
        content: "请输入提取金额",
        showCancel: false
      });
      return;
    }
    inputMoney = parseFloat(inputMoney).toFixed(2)
    console.log(inputMoney)
    // if (1 > inputMoney) {
    //   wx.showModal({
    //     title: "提示",
    //     content: "提取的金额不能少于1元",
    //     showCancel: false
    //   });
    //   return;
    // }
    if (10000 < inputMoney) {
      wx.showModal({
        title: "提示",
        content: "提取的金额不能超过10000元",
        showCancel: false
      });
      return;
    }
    inputMoney = parseInt(inputMoney * 100);
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/salesperformance/applyWithdraw",
      data: {
        cusmallToken: cusmallToken,
        amount: inputMoney
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        let data = res.data;
        if (data && 0 == data.ret) {
          that.setData({
            page: 1
          });
          that.setData({
            total: -1
          });
          that.loadData(false, that.data.page);
          wx.showModal({
            title: "提示",
            content: "提取成功",
            showCancel: false,
            success: function (res) {
              that.setData({
                modalSta: [true, true]
              });
              that.getPromoterAccount();

            }
          });
        } else {
          wx.showModal({
            title: "提示",
            content: data.msg,
            showCancel: false
          });
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    });

  },
  getPromoterAccount: function () {
    let that = this;

    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/salesperformance/getConfig",
      data: {
        cusmallToken: cusmallToken
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        let data = res.data;
        console.log(data)
        if (data && 0 == data.ret) {
          if (data.model.config) {
            let mpro = data.model.config;
            if (mpro.alipay && mpro.bankAccount) {
              that.setData({
                isBind: true
              })
            }
            that.setData({
              enableWithdrawMoney: data.model.enableMoney
            });
            that.setData({
              nickName: mpro.name
            });
            that.setData({
              tel: mpro.tel
            });
            that.setData({
              alipayAccount: mpro.alipay
            });
            that.setData({
              bankAccount: mpro.bankAccount
            });
            that.setData({
              bankName: mpro.bankName
            });
          }
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    });
  },
  queryWithdrawRecords: function (page) {
    let that = this;


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isLoading) {
      return;
    }
    this.data.page = 1;
    this.setData({
      total: -1
    });
    this.loadData(false, this.data.page);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoading) {
      return;
    }
    this.data.page = ++this.data.page;
    this.loadData(true, this.data.page);
  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {

  }
}))